package cn.szzsi._admin;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.kit.ModelUtils;
import cn.szzsi.common.model.Specification;
import cn.szzsi.common.model.SpecificationValue;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import org.apache.regexp.RE;

import java.util.Date;
import java.util.List;

/**
 * 商品规格处理
 */
public class SpecificationController extends BaseAdminController<Specification> {
    private String shopId;

    public void add(){
        shopId=getAttr("shopId");
        setAttr("shopId",shopId);
        render("specification_add.html");
    }

    public void save(){
        shopId=getAttr("shopId");
        Specification specification = getModel(Specification.class);
        specification.set("id",CommonUtil.getUUID()).set("shopId",shopId).set("createtime",new Date()).set("order",System.currentTimeMillis()).save();
        List<SpecificationValue> svl = ModelUtils.batchInjectModel(getRequest(),SpecificationValue.class,"specificationValues");
        for(SpecificationValue sv:svl){
            sv.set("id",CommonUtil.getUUID()).set("specification_id",specification.getStr("id")).set("timestamp",System.currentTimeMillis()).save();
        }
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }

    public void ajaxlist(){
        shopId=getAttr("shopId");
        List<Specification> ls = Specification.me.find("select * from mall_specification where shopId = ? and status=0 order by createtime desc",shopId);
        for(Specification s:ls){
            s.getValues();
        }
        renderJson(ls);
    }

    public void list(){
        String type = getPara("type");
        setAttr("type",type);
        render("specification_list.html");
    }

    public void edit(){
        shopId=getAttr("shopId");
        String specificationId=getPara("id");//商品规格名
        Specification specification=Specification.me.findById(specificationId);
        setAttr("specification",specification);//规格名

        //根据规格查询规名称查询规格值
        List<SpecificationValue>specificationValues=SpecificationValue.dao.getBySpecificationId(specificationId);
        setAttr("specificationValues",specificationValues);
        render("specification_edit.html");
    }

    public void update(){
        shopId=getAttr("shopId");
        Specification specification = getModel(Specification.class);
        specification.update();//更新规格
        List<SpecificationValue> specificationValues = ModelUtils.batchInjectModel(getRequest(),SpecificationValue.class,"specificationValues");
        for(SpecificationValue sv:specificationValues){
            if(StrKit.notBlank(sv.getId())){
                if("text".equals(specification.getType())){
                    sv.setImage(null);
                }
                sv.update();
            }else{
                sv.set("id",CommonUtil.getUUID()).set("specification_id",specification.getStr("id")).set("timestamp",System.currentTimeMillis()).save();
            }

        }
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }

    /**
     * 删除规格
     */
    public void del(){
        String id = getPara("id");
        Specification specification = Specification.me.findById(id);
        specification.setStatus(1);
        specification.update();
        Db.update("update mall_specification_value set status=1 where specification_id = ?",id);
        renderJson(Ret.ok());
    }

    /**
     * 删除规格值
     */
    public void delSpecificationValue(){
        String specificationValueId=getPara("specificationValueId");
        SpecificationValue.dao.deleteById(specificationValueId);
        renderJson(Ret.ok());
    }

}
